package com.house.Reborn.controller;

import com.house.Reborn.model.result.Result;
import com.house.Reborn.model.result.ResultSupport;
import com.house.Reborn.quartz.HelloJob;
import com.house.Reborn.quartz.QuartzManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by admin on 2017/6/26.
 */

@RestController
@Api(value = "调度类",tags = "调度接口")
@RequestMapping("/rb/task")
public class QuartzController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataController.class);
    public static String JOB_NAME = "动态任务调度";
    public static String TRIGGER_NAME = "动态任务触发器";
    public static String JOB_GROUP_NAME = "XLXXCC_JOB_GROUP";
    public static String TRIGGER_GROUP_NAME = "XLXXCC_JOB_GROUP";

    @RequestMapping(value = "/helloJob",method = RequestMethod.GET)
    @ApiOperation(value="启动调度", notes="启动一个HelloJob的调度", produces = "application/json")
    public Result exeHelloJob(){
        LOGGER.info("【系统启动】开始(每2秒输出一次)...");
        Result result = new ResultSupport();
        try{
            QuartzManager.addJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, HelloJob.class, "0/2 * * * * ?");
        }catch (RuntimeException e) {

            e.getMessage().toString();
            result.setCode("201");
            result.setMsg("改动cron调度quartz失败");
            LOGGER.error("启动调用HelloJob失败");

        }

        result.setCode("200");
        result.setMsg("改动cron调度quartz成功");
        return result;

    }

    @RequestMapping(value = "/modifyJob",method = RequestMethod.GET)
    @ApiOperation(value="修改调度时间", notes="修改并启动一个HelloJob的调度", produces = "application/json")
    public Result modifyJob(){
        LOGGER.info("【系统启动】(修改每5秒输出一次)...");
        Result result = new ResultSupport();
        try{
            QuartzManager.modifyJobTime(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, "0/5 * * * * ?");
        }catch (RuntimeException e) {

            e.getMessage().toString();
            result.setCode("201");
            result.setMsg("改动cron调度quartz失败");
            LOGGER.error("改动cron调度quartz失败");

        }
        result.setCode("200");
        result.setMsg("改动cron调度quartz成功");
        return result;

    }


    @RequestMapping(value = "/moveJob",method = RequestMethod.GET)
    @ApiOperation(value="移除调度", notes="移除HelloJob的调度", produces = "application/json")
    public Result moveJob(){
        LOGGER.info("【系统启动】(移除改调度任务)...");
        Result result = new ResultSupport();
        try{
            QuartzManager.removeJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME);
        }catch (RuntimeException e) {

            e.getMessage().toString();
            result.setCode("201");
            result.setMsg("移除调度helloJob失败");
            LOGGER.error("移除调度helloJob失败");

        }
        result.setCode("200");
        result.setMsg("移除调度helloJob成功");
        return result;

    }
    @RequestMapping(value = "/startAll",method = RequestMethod.GET)
    @ApiOperation(value="全部开始", notes="开始全部的调度任务", produces = "application/json")
    public Result startJobAll(){
        LOGGER.info("【系统启动】(开始全部的调度任务)...");
        Result result = new ResultSupport();
        try{
            QuartzManager.startJobs();
        }catch (RuntimeException e) {

            e.getMessage().toString();
            result.setCode("201");
            result.setMsg("开始全部调度失败");
            LOGGER.error("开始全部调度失败");

        }
        result.setCode("200");
        result.setMsg("移除调度helloJob成功");
        return result;

    }
    @RequestMapping(value = "/endAll",method = RequestMethod.GET)
    @ApiOperation(value="全部结束", notes="结束全部的调度任务", produces = "application/json")
    public Result shutDownJobAll(){
        LOGGER.info("【系统启动】(结束全部调度任务)...");
        Result result = new ResultSupport();
        try{
            QuartzManager.shutdownJobs();
        }catch (RuntimeException e) {

            e.getMessage().toString();
            result.setCode("201");
            result.setMsg("移除调度helloJob失败");
            LOGGER.error("移除调度helloJob失败");

        }
        result.setCode("200");
        result.setMsg("移除调度helloJob成功");
        return result;

    }
}
